let currentSlide = 0;
let slideInterval = null;
let slideIntervalAfterLast = null;
window.onload = function () {
    //轮播图
    var imgs = document.getElementById('box').getElementsByTagName("img");
    var lis = document.getElementById('box').getElementsByTagName("li");
    var i = 0;
    function switchImage(direction) {
        for (var j = 0; j < imgs.length; j++) {
            imgs[j].className = "";
            lis[j].className = "";
        }
        i += direction;
        // 如果索引超出范围，则循环回到第一张或最后一张  
        if (i >= imgs.length) { i = 0; }
        if (i < 0) { i = imgs.length - 1; }
        imgs[i].className = "cur";
        lis[i].className = "bg_red";
    }

    var interval = window.setInterval(function tab() {
        switchImage(1); //设置 1为跳转到下一张图片  
    }, 1000);
    document.querySelector('.prev').addEventListener('click', function () {
        clearInterval(interval);
        switchImage(-1); //上一张图片
    }); 
    document.querySelector('.next').addEventListener('click', function () {
        clearInterval(interval);
        switchImage(1); //下一张图片
    });
    // 当鼠标移入 box 时，清除定时器  
    document.getElementById('box').onmouseover = function () {
        clearInterval(interval);
    };

    // 当鼠标移出 box 时，重新设置定时器 
    document.getElementById('box').onmouseout = function () {
        interval = setInterval(tab, 2000);
    };

    // 倒计时
    function timeDown(endTime) {
        var nowTime = new Date();  //开始时间
        var endTimer = new Date(endTime); //结束时间
        var n = parseInt((endTimer - nowTime) / 1000);  //总的秒数    
        var Hours = parseInt((n / 3600) % 24);
        //单位数就在前面加个0
        if (Hours < 10) {
            Hours = '0' + Hours;
        }
        //单位数就在前面加个0
        var Minutes = parseInt((n / 60) % 60);; //分钟
        if (Minutes < 10) {
            Minutes = '0' + Minutes;
        }
        
        var Seconds = parseInt(n % 60);
        //单位数就在前面加个0
        if (Seconds < 10) {
            Seconds = '0' + Seconds;
        }
        var id = document.getElementById('timer');//找对象
        var span = document.querySelector('.timeDown').children; //获取span
        span[0].innerHTML = Hours;//修改
        span[1].innerHTML = Minutes;
        span[2].innerHTML = Seconds;
        setInterval(function () {
            timeDown(endTime);
        }, 1000);
    }
    timeDown('2024-6-22 18:00:00');

    //浮动广告
    var adContainer = document.getElementById('ad-container');
    var closeBtn = adContainer.querySelector('.close-btn');
    var w_height = window.innerHeight;
    var img_height = adContainer.offsetHeight;
    var max_top = w_height - img_height; // 确保广告不会移出视口
    var w_width = window.innerWidth;
    var img_width = adContainer.offsetWidth;
    var max_left = w_width - img_width;

    var x = 0; // x方向移动方向
    var y = 2; // y方向移动方向
    var isMouseOverAd = false; // 标记鼠标是否悬停在广告上
    // 添加关闭广告的功能
    closeBtn.addEventListener('click', function () {
        adContainer.style.display = 'none';
    });
    // 鼠标悬停时停止广告运动
    adContainer.addEventListener('mouseover', function () {
        isMouseOverAd = true;
    });
    adContainer.addEventListener('mouseout', function () {
        isMouseOverAd = false;
    });

    
    // 广告动画
    setInterval(function () {
        if (!isMouseOverAd) {
            var old_left = parseInt(adContainer.style.left || 0, 10);
            var new_left = old_left + 5 * x;
            var old_top = parseInt(adContainer.style.top || 0, 10);
            var new_top = old_top + 5 * y;

            // 检查边界条件并反转方向
            if (new_top >= max_top || new_top < 0) {
                y = -1 * y;
            }

            if (new_left >= max_left || new_left < 0) {
                x = -1 * x;
            }

            // 更新广告位置
            adContainer.style.left = new_left + 'px';
            adContainer.style.top = new_top + 'px';
        }
    }, 100);
    //一键换肤
    var skinToggleBtn = document.getElementById('skinToggleBtn');
    var skinSelector = document.getElementById('skinSelector');
    var pics = skinSelector.querySelectorAll('img');
    var cancelSkinBtn = document.getElementById('cancelSkinBtn');

    skinToggleBtn.onclick = function () {
        skinSelector.classList.toggle('visible'); // 切换显示/隐藏状态  
        skinSelector.classList.toggle('hidden');  // 同时切换隐藏类以处理过渡效果  
    };

    pics.forEach(function (pic) {
        pic.onclick = function () {
            document.body.style.backgroundImage = 'url(' + this.src + ')';
            skinSelector.classList.remove('visible'); // 点击图片后隐藏列表  
            skinSelector.classList.add('hidden');
        };
    });

    cancelSkinBtn.onclick = function () {
        document.body.style.backgroundImage = 'none'; // 取消换肤，恢复原来的背景颜色
    };
}
